//
//  PywInputPayPwdController.m
//  ANB_PasswordView_Demo
//
//  Created by smallwave on 16/7/26.
//  Copyright © 2016年 yyft. All rights reserved.
//

#import "PywInputPayPwdController.h"
#import "PywMasonry.h"
#import "PywPasswordView.h"
#import "UIImage+Pyw.h"
#import "PywCheckPayPwdApi.h"

@interface PywInputPayPwdController ()

@property (nonatomic, strong) UILabel *titleLabel;          // 请输入支付密码
@property (nonatomic, strong) UIButton *closeBtn;           // 关闭按钮
@property (nonatomic, strong) UILabel *infoLabel;           // 支付信息Label
@property (nonatomic, strong) UIView *contentView;          // 内容View
@property (nonatomic, strong) PywPasswordView *pwdTextView; // 密码输人框View

@property (nonatomic, copy) NSString *failPayAmount;       // 实际所需支付的金额
@property (nonatomic, copy) NSString *couponPayAmount;     // 代金券已经抵扣金额

@property (nonatomic, strong) PywCheckPayPwdApi *checkPayPwdApi;

@end

@implementation PywInputPayPwdController
KeyBoardHelper(50)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}
- (void)setUp{
    self.view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.7];
    [self addKeyWordView];
}
- (void)addKeyWordView{
    [self.view addSubview:self.contentView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.closeBtn];
    [self.contentView addSubview:self.infoLabel];
    [self.contentView addSubview:self.pwdTextView];
    
    self.pwdTextView.elementCount = 6;
    self.pwdTextView.elementMargin = 0;
    self.pwdTextView.elementColor = [UIColor grayColor];
    
    WEAK_SELF;;
    self.pwdTextView.passwordBlock = ^(NSString *password){
        PLog(@"%@",password);
        
        if (password.length == weakSelf.pwdTextView.elementCount) {
            [weakSelf.checkPayPwdApi requestCheckPayPwdWithPayPwd:password success:^(NSString *status, id anyObjct) {
                
                [weakSelf disMiss];
                if (weakSelf.checkPayPwdComplete) {
                    weakSelf.checkPayPwdComplete();
                }
            } fail:^(NSError *error) {
                
            }];
        }
    };
    
    [self.contentView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.center.offset(0);
        if (IS_IPHONE) {
            make.width.equalTo(@(SCREEN_MIN_LENGTH * 0.9));
        } else {
            make.width.equalTo(@370);
        }
    }];
    
    [self.titleLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(weakSelf.contentView.mas_top);
        make.width.equalTo(weakSelf.contentView.mas_width);
        make.height.equalTo(@40);
    }];
    
    [self.closeBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.titleLabel.mas_centerY);
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-5);
        make.width.height.equalTo(weakSelf.titleLabel.mas_height);
    }];
    
    [self.infoLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(20);
        make.width.equalTo(weakSelf.contentView.mas_width).multipliedBy(0.85);
    }];
    
    [self.pwdTextView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(weakSelf.infoLabel.mas_bottom).offset(20);
        make.width.equalTo(weakSelf.infoLabel.mas_width);
        make.height.equalTo(weakSelf.titleLabel.mas_height);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom).offset(-30);
    }];
    
    [self.view setNeedsLayout];
}

- (void)show {
    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.contentView.alpha = 1;
    } completion:nil];
    
    [self addKeyAnimation];
}
- (void)addKeyAnimation {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.calculationMode = kCAAnimationCubic;
    animation.values = @[@1.07,@1.06,@1.05,@1.03,@1.02,@1.01,@1.0];
    animation.duration = 0.4;
    [self.contentView.layer addAnimation:animation forKey:@"transform.scale"];
}
- (void)disMiss {
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (void)showInViewController:(UIViewController *)controller
               failPayAmount:(NSString *)failPayAmount
             couponPayAmount:(NSString *)couponPayAmount
         checkPayPwdComplete:(CheckPayPwdComplete)checkPayPwdComplete{
    self.failPayAmount = failPayAmount;
    self.couponPayAmount = couponPayAmount;
    self.checkPayPwdComplete = checkPayPwdComplete;
    
    if (controller) {
        [controller addChildViewController:self];
        [controller.view addSubview:self.view];
        [self show];
        [self.view mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
}

#pragma mark - Setter & Getter
- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = UIColorFromRGB(0xFFFFFF);
        _contentView.layer.cornerRadius = 20;
        _contentView.layer.masksToBounds = YES;
    }
    return _contentView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = UIColorFromRGB(0xBEBEBE);
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"请输入支付密码";
    }
    return _titleLabel;
}
- (UIButton *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _closeBtn.backgroundColor = UIColorFromRGB(0xBEBEBE);
        [_closeBtn setImage:[UIImage imageWithName:@"icon_close_normal"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
- (UILabel *)infoLabel{
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.backgroundColor = [UIColor clearColor];
        _infoLabel.textAlignment = NSTextAlignmentCenter;
        _infoLabel.numberOfLines = 0;
        
        NSString *infoMessage = [NSString stringWithFormat:@"%@\n\n代金券已抵扣%@", self.failPayAmount,self.couponPayAmount];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:infoMessage];
        // 实际所需支付的金额
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:28] range:NSMakeRange(0, self.failPayAmount.length)];
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, self.failPayAmount.length)];
        
        // 代金券已经抵扣金额
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(self.failPayAmount.length, attStr.length - self.failPayAmount.length)];
        
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(self.failPayAmount.length, attStr.length - (self.failPayAmount.length + self.couponPayAmount.length))];
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(attStr.length - self.couponPayAmount.length, self.couponPayAmount.length)];
        
        _infoLabel.attributedText = attStr;
    }
    return _infoLabel;
}
- (PywPasswordView *)pwdTextView{
    if (!_pwdTextView) {
        _pwdTextView = [[PywPasswordView alloc] init];
        _pwdTextView.backgroundColor = [UIColor whiteColor];
        _pwdTextView.layer.cornerRadius = 10;
        _pwdTextView.layer.masksToBounds = YES;
        _pwdTextView.layer.borderWidth = 1;
        _pwdTextView.layer.borderColor = [UIColor grayColor].CGColor;
    }
    return _pwdTextView;
}
- (PywCheckPayPwdApi *)checkPayPwdApi{
    if (!_checkPayPwdApi) {
        _checkPayPwdApi = [PywCheckPayPwdApi manager];
    }
    return _checkPayPwdApi;
}

@end
